/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.netconsole.action;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.SessionFactory;

import org.netconsole.dao.UserDAO;
import org.netconsole.dao.UserDAOImpl;
import org.netconsole.model.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;

/**
 *
 * @author aurel
 */
public class UserAction extends ActionSupport implements Action, ModelDriven<User>, ServletContextAware{

    private User user = new User();
    private List<User> lst;
    private ServletContext ctx;
    private Map session = ActionContext.getContext().getSession();
    private String msgs;

    @Override
    public String execute() throws Exception {
        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        UserDAO userDAO = new UserDAOImpl(sf);
        User u = userDAO.getUserByCredentials(user.getId(), user.getPwd());
        if (u != null) {
            session = ActionContext.getContext().getSession();
            session.put("connecte", true);
            session.put("id", u.getId());
            session.put("droits", u.getDroits());
            session.put("error", "none");
            msgs = "Hello";
        return SUCCESS;
        } else {
            session.put("error", "errorLogin");
            System.out.print(msgs);
            return ERROR;
    }
    }

    @Override
    public User getModel() {
        return user;
    }

    @Override
    public void setServletContext(ServletContext cs) {
        this.ctx = cs;
    }
    
    public List<User> getLst() {
        return lst;
    }

    public void setLst(List<User> lst) {
        this.lst = lst;
    }
    
    public String signup() throws Exception {
        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        UserDAO userDAO = new UserDAOImpl(sf);
        User userDB = userDAO.getUserByEmail(user.getEmail());
        if (userDB == null){
            userDB = userDAO.insertUser(user);
            return SUCCESS;
        }
        else {
            session.put("error", "errorSignup");
            System.out.print(msgs);
            return ERROR;}
    }
    
    public String listUsers() throws Exception {
        SessionFactory sf =(SessionFactory) ctx.getAttribute("SessionFactory");
        UserDAO userDAO = new UserDAOImpl(sf);
        lst = userDAO.listUsers();

        System.out.println(lst.size() + " userAction !! \n\n");
        for (int i=0; i<lst.size(); i++){
            System.out.println(lst.get(i).toString());
        }
        return SUCCESS;
    }
    
    public String DeleteSession() {
        session = ActionContext.getContext().getSession();
        session.clear();
        return SUCCESS;
    }
    
    public String modifForm() throws Exception {
        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        UserDAO userDAO = new UserDAOImpl(sf);
	User userDB = userDAO.getUserByEmail(user.getEmail());
        if (userDB != null){
            return SUCCESS;
        }
        else{
            return ERROR;
        }
    }  
    
    public String modifUser() throws Exception {
        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        UserDAO userDAO = new UserDAOImpl(sf);
	User userDB = userDAO.modifUser(user);
        listUsers();
        return SUCCESS;
    }
}
